package com.lj.api.v1.c;

import com.alibaba.fastjson.JSONObject;
import com.lj.api.common.CodeMsg;
import com.lj.api.common.Result;
import com.lj.api.common.WebUtil;
import com.lj.api.tools.AccessoryTools;
import com.lj.mcore.entity.*;
import com.lj.mcore.execption.OutOfStockException;
import com.lj.mcore.service.*;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/c")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;

    @Autowired
    IUserService userService;

    @Autowired
    IStoreService storeService;

    @Autowired
    IGoodsClassService goodsClassService;

    @Autowired
    IGoodsBrandService goodsBrandService;

    @Autowired
    IAccessoryService accessoryService;

    @Autowired
    IGoodsSpecificationService goodsSpecificationService;

    @Autowired
    IGoodsCartService goodsCartService;

    @Autowired
    IStoreCartService storeCartService;


    @RequestMapping("/goodsList")
    public String goodslist(String currentPage,String goods_name){
        int page = CommUtils.null2Int(currentPage)==0?1:CommUtils.null2Int(currentPage);
        Map query = new HashMap();
        query.put("goods_name",CommUtils.null2String(goods_name));
        PageBean pagebean = goodsService.getList(15,page,query);
        List<Goods> list = pagebean.getList();

        List list1 = new ArrayList();
        for(Goods goods:list){
            Map map = new HashMap();
            map.put("id",goods.getId().longValue());
            map.put("goods_name",goods.getGoods_name());

            //品牌
            GoodsBrand goods_bands= goods.getGoods_brand();
            if (goods_bands!=null){
                map.put("dbid",goods_bands.getId().longValue());
                map.put("bands_name",goods_bands.getName());
            }

            //店铺
            Store Store = goods.getGoods_store();
            if (Store!=null){
                map.put("sid",Store.getId().longValue());
                map.put("storeName",Store.getStore_name());
            }

            //商品分类
            GoodsClass goodsClass = goods.getGc();
            if (goodsClass!=null){
                map.put("gc_name",goodsClass.getClassName());
            }

            //0 已上架 1 仓库中 -1 下架 -2 违规下架
            map.put("goods_status",goods.getGoods_status());
            //查看次数
            map.put("goods_click",goods.getGoods_click());
            //推荐
            map.put("store_recommend",goods.isStore_recommend());
            list1.add(map);
        }

        Map info = new HashMap();
        info.put("objs", JSONObject.toJSON(list1));
        info.put("totalPage", pagebean.getTotalPage());
        info.put("pageSize", pagebean.getPageSize());
        info.put("rows", pagebean.getAllRow());
        info.put("currentPage", pagebean.getCurrentPage());
        return Result.ok(info);
    }

    @RequestMapping("/add_goods_finish")
    public String add_goods_finish(String id,String goodsName,String goods_price,String store_price,String goods_choice_type,String inventory_type,
                            String goods_inventory,String goods_serial,String goods_volume,String goods_weight,String goods_transfee,
                            String goods_brand_id,String goods_details,String goods_status,String goods_recommend,String seo_description,
                            String seo_keywords,String goods_class_id,String uid,String[] aids,String gtypes,String tableData){

        if (WebUtil.checkEmpty(uid,goodsName,goods_price,store_price)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        User user = userService.getObjById(CommUtils.null2Long(uid));
        Store store = user.getStore();

        if (store==null){
            return Result.fail(CodeMsg.NO_ACCESS_AUTH);
        }

        Goods goods =null;
        //新增商品
        if (CommUtils.null2String(id).equals("")){
            goods = new Goods();
            goods.setAddTime(new Date());
        }else {
            goods = goodsService.getObjById(CommUtils.null2Long(id));
        }

        goods.setGoods_name(goodsName);
        goods.setGoods_price(new BigDecimal(goods_price));
        goods.setStore_price(new BigDecimal(store_price));
        goods.setGoods_choice_type(CommUtils.null2Int(goods_choice_type));
        goods.setInventory_type(CommUtils.null2String(inventory_type));
        //库存
        goods.setGoods_inventory(CommUtils.null2Int(goods_inventory));
        //商品货号
        goods.setGoods_serial(CommUtils.null2String(goods_serial));
        if (!CommUtils.null2String(goods_volume).equals("")){
            goods.setGoods_volume(new BigDecimal(goods_volume));
        }

        if (!CommUtils.null2String(goods_weight).equals("")){
            goods.setGoods_weight(new BigDecimal(goods_weight));
        }
        goods.setGoods_transfee(CommUtils.null2Int(goods_transfee));
        goods.setGoods_details(CommUtils.null2String(goods_details));
        goods.setGoods_status(CommUtils.null2Int(goods_status));
        goods.setGoods_recommend(CommUtils.null2Boolean(goods_recommend));
        goods.setSeo_keywords(CommUtils.null2String(seo_keywords));
        goods.setSeo_description(CommUtils.null2String(seo_description));

        //添加商品类别
        goods.setGc(goodsClassService.getObjById(CommUtils.null2Long(goods_class_id)));

        //照片处理
        if (aids!=null && aids.length>0){
            List<Accessory> accessories = new ArrayList<>();
            for (int i = 0 ;i<aids.length;i++){
                Accessory accessory = accessoryService.getObjById(Long.valueOf(aids[i]));
                if (i==0){
                    goods.setGoods_main_photo(accessory);
                }
                accessories.add(accessory);
            }

            goods.setGoods_photos(accessories);
        }else{
            goods.setGoods_photos(null);
            goods.setGoods_main_photo(null);
        }

        //关联品牌
        if(!CommUtils.null2String(goods_brand_id).equals("")){
            goods.setGoods_brand(goodsBrandService.getObjById(CommUtils.null2Long(goods_brand_id)));
        }

        //

        if (CommUtils.null2String(id).equals("")){

            //关联商铺
            goods.setGoods_store(store);

            //商品规格
            /*if (gtypes!=null && gtypes.length>0){*/
            if (!CommUtils.null2String(gtypes).equals("")){
                JSONArray jsonArray = JSONArray.fromObject(gtypes);
                goods.setSpecifications(cre_specification(jsonArray));
                goods.setHas_spec_items(1);
            }

            List<GoodsStock> stocks = new ArrayList<>();
            if (!CommUtils.null2String(tableData).equals("")){
                JSONArray jsonArray = JSONArray.fromObject(tableData);

                for(int i = 0 ;i< jsonArray.size();i++){
                    GoodsStock goodsStock = new GoodsStock();
                    net.sf.json.JSONObject object=jsonArray.getJSONObject(i);
                    goodsStock.setAddTime(new Date());
                    goodsStock.setName(object.getString("name"));
                    goodsStock.setIdPath(object.getString("idPath"));
                    goodsStock.setType(0);
                    if (goods.getInventory_type().equals("spec")){

                        if (!CommUtils.null2String(object.getString("stock")).equals("")){
                            goodsStock.setStock(object.getInt("stock"));
                        }

                        if (!CommUtils.null2String(object.getString("price")).equals("")){
                            goodsStock.setPrice(new BigDecimal(object.getString("price")));
                        }
                        goodsStock.setType(1);
                    }

                    if (!CommUtils.null2String(object.getString("serial")).equals("")){
                        goodsStock.setSerial(object.getString("serial"));
                    }
                    stocks.add(goodsStock);
                }
            }
            goodsService.saveWithStock(goods,stocks);
        }else {

            /**
             * 更新操作，为了防止扣减库存操作准确，只对。GoodsStorck 类中 stock、price、serial可进行更新。 商品表不受限制。
             *
             */
            List<GoodsStock> stocks = new ArrayList<>();
            if (!CommUtils.null2String(tableData).equals("")){
                JSONArray jsonArray = JSONArray.fromObject(tableData);
                for(int i = 0 ;i< jsonArray.size();i++){

                    net.sf.json.JSONObject object=jsonArray.getJSONObject(i);
                    GoodsStock goodsStock =goodsStockService.getObjById(object.getLong("id"));

                    if (goods.getInventory_type().equals("spec")){

                        if (!CommUtils.null2String(object.getString("stock")).equals("")){
                            goodsStock.setStock(object.getInt("stock"));
                        }

                        if (!CommUtils.null2String(object.getString("price")).equals("")){
                            goodsStock.setPrice(new BigDecimal(object.getString("price")));
                        }
                        goodsStock.setType(1);
                    }

                    if (!CommUtils.null2String(object.getString("serial")).equals("")){
                        goodsStock.setSerial(object.getString("serial"));
                    }
                    stocks.add(goodsStock);
                }
            }

            /*try {
                goodsService.updateWithStock(goods,stocks);
            } catch (OutOfStockException e) {
                e.printStackTrace();
                //库存不足
                return Result.fail(CodeMsg.OUT_OF_STOCK);
            } catch (OptimisticLockingFailureException e){
                e.printStackTrace();
                //更新失败，乐观锁机制
                return Result.fail(CodeMsg.SYS_BUSY);
            } catch (Exception e){
                e.printStackTrace();
            }*/
            goodsService.updateWithStock(goods,stocks);
        }
        return  Result.ok();
    }

    public List<GoodsSpecification> cre_specification(JSONArray jsonArray){
        List<GoodsSpecification> cre_specification = new ArrayList();

        for (int i = 0;i<jsonArray.size();i++){
            net.sf.json.JSONObject obj = jsonArray.getJSONObject(i);
            GoodsSpecification specification = new GoodsSpecification();
            List<GoodsSpecProperty> properties = new ArrayList();
            specification.setName(obj.getString("gt_name"));
            JSONArray vals = obj.getJSONArray("vals");
            for (int y =0;y<vals.size();y++){
                net.sf.json.JSONObject val = vals.getJSONObject(y);
                GoodsSpecProperty specProperty = new GoodsSpecProperty();
                specProperty.setValue(val.getString("name"));
                if (!CommUtils.null2String(val.getString("aid")).equals("")){
                    Accessory accessory = accessoryService.getObjById(val.getLong("aid"));
                    specProperty.setSpecImage(accessory);
                }
                specProperty.setAddTime(new Date());
                properties.add(specProperty);
            }
            specification.setProperties(properties);

            cre_specification.add(specification);
        }
        return cre_specification;
    }

    @RequestMapping("/g_info")
    public String g_info(String id){

        if (WebUtil.checkEmpty(id)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }
        Goods goods = goodsService.getObjById(Long.valueOf(id));
        if (goods==null || goods.isDeleteStatus()){
            return Result.fail(CodeMsg.ILLEGAL_PARAMS);
        }

        Map map = new HashMap();
        map.put("id",goods.getId());
        map.put("goodsName",goods.getGoods_name());
        map.put("goods_price",goods.getGoods_price());
        map.put("store_price",goods.getStore_price());
        map.put("goods_choice_type",goods.getGoods_choice_type());
        map.put("inventory_type",goods.getInventory_type());
        map.put("goods_serial",goods.getGoods_serial());
        map.put("goods_volume",goods.getGoods_volume());
        map.put("goods_weight",goods.getGoods_weight());
        map.put("goods_transfee",goods.getGoods_transfee());
        if (goods.getGoods_brand()!=null){
            map.put("goods_brand_id",goods.getGoods_brand().getId());
        }else{
            map.put("goods_brand_id","");
        }

        map.put("goods_details",goods.getGoods_details());
        map.put("goods_status",goods.getGoods_status());
        map.put("goods_recommend",goods.isGoods_recommend()?1:0);
        map.put("seo_keywords",goods.getSeo_keywords());
        map.put("seo_description",goods.getSeo_description());
        map.put("goods_inventory",goods.getGoods_inventory());
        map.put("has_spec_items",goods.getHas_spec_items());
        if (goods.getGc()!=null){
            map.put("goods_class_id",goods.getGc().getId());
        }else{
            map.put("goods_class_id","");
        }

        if(goods.getGoods_brand()!=null){
            map.put("goods_brand_id",goods.getGoods_brand().getId());
        }else{
            map.put("goods_brand_id","");
        }

        List<Accessory> accessories = goods.getGoods_photos();
        List list = new ArrayList();
        if (accessories.size()>0){
            for(Accessory accessory : accessories){
                Map map1 = new HashMap();
                map1.put("aid",accessory.getId());
                map1.put("url", AccessoryTools.getProcessURL_QN(accessory,2));
                list.add(map1);
            }
        }
        map.put("aids",list);

        List<GoodsSpecification> specifications = goods.getSpecifications();
        List gtypes = new ArrayList();
        for (GoodsSpecification specification :specifications){
            Map map1 = new HashMap();
            map1.put("id",specification.getId());
            map1.put("gt_name",specification.getName());
            List<GoodsSpecProperty> properties = specification.getProperties();
            List list1 = new ArrayList();
            for(GoodsSpecProperty specProperty:properties){
                Map map2 = new HashMap();
                map2.put("id",specProperty.getId()+"");
                map2.put("name",specProperty.getValue());
                Accessory accessory = specProperty.getSpecImage();
                if (accessory!=null){
                    map2.put("aid",accessory.getId()+"");
                    map2.put("img",AccessoryTools.getProcessURL_QN(accessory,1));
                }
                list1.add(map2);
            }

            map1.put("vals",list1);

            gtypes.add(map1);
        }
        map.put("gtypes",gtypes);

        Map query = new HashMap();
        query.put("gid",goods.getId());
        List<GoodsStock> stocks = goodsStockService.query(IGoodsStockService.GET_GOOD_STOCK,query,-1,-1);
        List tableData = new ArrayList();
        for(GoodsStock goodsStock :stocks){
            Map map1 = new HashMap();
            map1.put("id",goodsStock.getId()+"");
            map1.put("name",goodsStock.getName());
            map1.put("stock",goodsStock.getStock()!=null?goodsStock.getStock()+"":"");
            map1.put("price",goodsStock.getPrice()!=null?goodsStock.getPrice()+"":"");
            map1.put("serial",goodsStock.getSerial()!=null?goodsStock.getSerial():"");
            System.out.println(goodsStock.getKey());
            map1.put("key",goodsStock.getKey()!=null?goodsStock.getKey():"");
            tableData.add(map1);
        }

        map.put("tableData",tableData);
        return Result.ok(map);
    }

    @RequestMapping("/check_goods_serial")
    public String check_goods_serial(String goods_serial){
        if (WebUtil.checkEmpty(goods_serial)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }
        int num = goodsService.checkGoodsSerial(goods_serial);
        int isExist = (num==0)?0:1;
        Map map = new HashMap();
        map.put("isExist",isExist);
        return Result.ok(map);
    }

    @Autowired
    IGoodsStockService goodsStockService;

    @RequestMapping("/add")
    public String gcList(){
        GoodsStock goodsStock = new GoodsStock();
        goodsStock.setAddTime(new Date());
        goodsStock.setGoods(goodsService.getObjById(98466L));
        goodsStock.setType(0);
        goodsStock.setStock(2);
        goodsStockService.save(goodsStock);
        return Result.ok();
    }

    @RequestMapping("/u")
    public String u(String id,String num){
        GoodsStock goodsStock = goodsStockService.getObjById(Long.valueOf(id));
        try {
            Thread.currentThread().sleep(5000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        goodsStock.setStock(Integer.valueOf(num));
        try {
            goodsStockService.update(goodsStock);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    @RequestMapping("/u1")
    public String u1(String id,String num){
        GoodsStock goodsStock = goodsStockService.getObjById(Long.valueOf(id));
        goodsStock.setStock(Integer.valueOf(num));
        try {
            goodsStockService.update(goodsStock);
        }catch (OutOfStockException e) {
            e.printStackTrace();
        } catch (ObjectOptimisticLockingFailureException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok();
    }


    @RequestMapping("/cart_list")
    public String cart_list(String currentPage,String pageSize,String uid){

        if (WebUtil.checkEmpty(uid,currentPage,currentPage)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        int page = CommUtils.null2Int(currentPage)==0?1:CommUtils.null2Int(currentPage);

        Map query = new HashMap();
        query.put("uid",CommUtils.null2Long(uid));

        PageBean pagebean = goodsService.getList(CommUtils.null2Int(pageSize),page,query);
        List<GoodsCart> list = pagebean.getList();

        List list1 = new ArrayList();
        for(GoodsCart goodsCart:list){
            Map map = new HashMap();
            map.put("id",goodsCart.getId());

            //品牌
            Goods goods= goodsCart.getGoods();

            //0 已上架 1 仓库中 -1 下架 -2 违规下架
            if (goods.getGoods_status()!=0){
                map.put("goods_status",-1);
            }else{
                map.put("goods_status",1);
            }

            map.put("goodsName",goods.getGoods_name());
            if (goods.getGoods_main_photo()!=null){
                map.put("img",AccessoryTools.getProcessURL_QN(goods.getGoods_main_photo(),2));
            }else{
                map.put("img","");
            }
            map.put("count",goodsCart.getCount());
            map.put("price",goodsCart.getPrice().longValue());
            map.put("buy_type",goodsCart.getCart_type());
            list1.add(map);
        }

        Map info = new HashMap();
        info.put("objs", JSONObject.toJSON(list1));
        info.put("totalPage", pagebean.getTotalPage());
        info.put("pageSize", pagebean.getPageSize());
        info.put("rows", pagebean.getAllRow());
        info.put("currentPage", pagebean.getCurrentPage());
        return Result.ok(info);
    }

    @RequestMapping("/add_cart")
    public String add_cart(String uid,String gid,String buy_type,String count,String price,String key,String key_name){
        if (WebUtil.checkEmpty(uid,gid,price,count,buy_type)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        double sellingPrice = new BigDecimal(price).doubleValue();

        if (Integer.valueOf(count).intValue()<=0 || sellingPrice<0.01){
            return Result.fail(CodeMsg.ILLEGAL_PARAMS);
        }


        Goods goods = goodsService.getObjById(Long.valueOf(gid));
        if (goods==null){
            return Result.fail(CodeMsg.ILLEGAL_PARAMS);
        }


        int goods_status = goods.getGoods_status();
        if (goods_status !=0){
            return Result.fail(CodeMsg.OFF_SHELF);
        }

        // 检查库存不足情况
        String inventory_type = goods.getInventory_type();
        //总库存
        int inventory = goods.getGoods_inventory();

        if ((inventory-Integer.valueOf(count).intValue())<0){
            return Result.fail(CodeMsg.OUT_OF_STOCK);
        }

        GoodsStock goodsStock = null;
        int has_spec_items = goods.getHas_spec_items();
        if (has_spec_items==1){
            if (WebUtil.checkEmpty(key,key_name)){
                return Result.fail(CodeMsg.PARAMETER_ISNULL);
            }

            Map map = new HashMap();
            map.put("gid",Long.valueOf(gid));
            map.put("key",key);
            map.put("deleteStatus",false);

            List<GoodsStock> goodsStocks = goodsStockService.query(IGoodsStockService.GET_GOOD_STOCK_BY_KEY,map,0,1);
            if (goodsStocks.size()<=0){
                return Result.fail(CodeMsg.ILLEGAL_PARAMS);
            }else{
                goodsStock = goodsStocks.get(0);
            }
        }


        //规格配置库存检查
        if (inventory_type.equals("spec")) {

            //规格配置模式下，检查库存情况
            if((goodsStock.getStock().intValue()-Integer.valueOf(count).intValue())<0){
                return Result.fail(CodeMsg.OUT_OF_STOCK);
            }
        }


        //正常购买（未参加活动）
        if (buy_type.equals("1")){

            if (inventory_type.equals("all")) {
                if (goods.getStore_price().doubleValue() != sellingPrice) {
                    return Result.fail(CodeMsg.PRICE_IS_CHANGED);
                }
            }else{

                if (goodsStock.getPrice().doubleValue()!=sellingPrice){
                    return Result.fail(CodeMsg.PRICE_IS_CHANGED);
                }
            }
        }

        //查询商品是否已经添加进购物车
        GoodsCart goodsCart = null;
        if (has_spec_items==1) {
            goodsCart =goodsCartService.goodsIsExistInCard(Long.valueOf(gid).longValue(), Long.valueOf(uid).longValue(),goodsStock.getId().longValue());
        }else {
            goodsCart = goodsCartService.goodsIsExistInCard(Long.valueOf(gid).longValue(), Long.valueOf(uid).longValue(),null);
        }

        if (goodsCart==null) {
            goodsCart = new GoodsCart();
            goodsCart.setGoods(goods);
            goodsCart.setCount(Integer.valueOf(count));
            goodsCart.setPrice(new BigDecimal(price));
            goodsCart.setCart_type(buy_type);
            goodsCart.setUser(userService.getObjById(Long.valueOf(uid)));

            if (has_spec_items == 1) {
                goodsCart.setGoodsStock(goodsStock);
                goodsCart.setKey(goodsStock.getKey());
                goodsCart.setSpec_info(goodsStock.getName());
            }

            goodsCart.setStore(goods.getGoods_store());
            goodsCartService.saveWithStoreCart(goodsCart);
        }else{
            //更新价格
            goodsCart.setPrice(new BigDecimal(price));
            //更新购买数量
            goodsCart.setCount(goodsCart.getCount()+Integer.valueOf(count).intValue());
            goodsCartService.UpdateWithStoreCart(goodsCart);
        }
        return Result.ok();
    }

    @RequestMapping("/cart_index")
    public String cart_index(String uid){
        if (WebUtil.checkEmpty(uid)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        Map query = new HashMap();
        query.put("deleteStatus",false);
        query.put("uid",Long.valueOf(uid));
        //查询最近有操作的10个商铺购车信息
        List<StoreCart> storeCarts = storeCartService.query(IStoreCartService.CART_INDEX,query,0,10);

        List list = new ArrayList();
        for (StoreCart storeCart : storeCarts){
            Map map = new HashMap();
            map.put("scid",storeCart.getId());
            map.put("storeName",storeCart.getStore().getStore_name());
            map.put("total_price",storeCart.getTotal_price().doubleValue());
            List<GoodsCart> goodsCarts = storeCart.getGoodsCarts();
            List list1 = new ArrayList();
            for (GoodsCart goodsCart :goodsCarts){
                Map map1 = new HashMap();
                map1.put("gcid",goodsCart.getId());
                map1.put("gid",goodsCart.getGoods().getId());
                map1.put("goodsName",goodsCart.getGoods().getGoods_name());
                map1.put("price",goodsCart.getPrice().doubleValue());
                map1.put("count",goodsCart.getCount());
                map1.put("key",CommUtils.null2String(goodsCart.getKey()));
                map1.put("key_name",CommUtils.null2String(goodsCart.getSpec_info()));
                double subtotal = CommUtils.mul(goodsCart.getPrice().doubleValue(),goodsCart.getCount());
                map1.put("subtotal",subtotal);
                list1.add(map1);
            }
            map.put("goodsCart",list1);
            list.add(map);
        }

        return Result.ok(list);
    }


    @RequestMapping("/settle")
    public String settle(String[] gcids,String uid){

        if (gcids==null || gcids.length<=0){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        if (WebUtil.checkEmpty(uid)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }
        Map map = new HashMap();
        double total_price = 0.0;
        List list = new ArrayList();
        for (String gcid:gcids){
            GoodsCart goodsCart = goodsCartService.getObjById(Long.valueOf(gcid));

            String rszult = check_goods(goodsCart);
            if (!rszult.equals("ok")){
                return  rszult;
            }

            total_price+= CommUtils.mul(goodsCart.getPrice().doubleValue(),goodsCart.getCount());
            list.add(goodsCart);
        }
        map.put("goods",list);
        map.put("total_price",total_price);
        return Result.ok(map);
    }

    @RequestMapping("/placeOrder")
    public String placeOrder(String[] gcids,String uid,String addressId){

        if (gcids==null || gcids.length<=0){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        if (WebUtil.checkEmpty(uid,addressId)){
            return Result.fail(CodeMsg.PARAMETER_ISNULL);
        }

        double total_price = 0.0;

        return Result.ok();
    }

    public String check_goods(GoodsCart goodsCart){

        Goods goods = goodsCart.getGoods();
        int count = goodsCart.getCount();
        if (goods==null){
            return Result.fail(CodeMsg.ILLEGAL_PARAMS);
        }


        int goods_status = goods.getGoods_status();
        if (goods_status !=0){
            return Result.fail(CodeMsg.OFF_SHELF);
        }

        // 检查库存不足情况
        String inventory_type = goods.getInventory_type();
        //总库存
        int inventory = goods.getGoods_inventory();

        if ((inventory-count)<0){
            return Result.fail(CodeMsg.OUT_OF_STOCK);
        }

        GoodsStock goodsStock = null;
        int has_spec_items = goods.getHas_spec_items();
        if (has_spec_items==1){

            String key = goodsCart.getKey();
            String key_name = goodsCart.getSpec_info();

            if (WebUtil.checkEmpty(key,key_name)){
                return Result.fail(CodeMsg.PARAMETER_ISNULL);
            }

            Map map = new HashMap();
            map.put("gid",goods.getId());
            map.put("key",key);
            map.put("deleteStatus",false);

            List<GoodsStock> goodsStocks = goodsStockService.query(IGoodsStockService.GET_GOOD_STOCK_BY_KEY,map,0,1);
            if (goodsStocks.size()<=0){
                return Result.fail(CodeMsg.ILLEGAL_PARAMS);
            }else{
                goodsStock = goodsStocks.get(0);
            }
        }


        //规格配置库存检查
        if (inventory_type.equals("spec")) {

            //规格配置模式下，检查库存情况
            if((goodsStock.getStock().intValue()-Integer.valueOf(count).intValue())<0){
                return Result.fail(CodeMsg.OUT_OF_STOCK);
            }
        }

        double sellingPrice = goodsCart.getPrice().doubleValue();
        //正常购买（未参加活动）
        if (goodsCart.getCart_type().equals("1")){

            if (inventory_type.equals("all")) {
                if (goods.getStore_price().doubleValue() != sellingPrice) {
                    return Result.fail(CodeMsg.PRICE_IS_CHANGED);
                }
            }else{

                if (goodsStock.getPrice().doubleValue()!=sellingPrice){
                    return Result.fail(CodeMsg.PRICE_IS_CHANGED);
                }
            }
        }

        return "ok";

    }


}
